package com.qyhl.cloud.webtv.yunshang.app;

import android.app.Application;
import android.content.Context;
import androidx.multidex.MultiDex;

import com.qyhl.webtv.basiclib.base.BaseAppConfig;
import com.qyhl.webtv.basiclib.base.BaseApplication;

/**
 * 宿主App
 *
 * @author helin
 * @date  2019年2月22日17:23:58
 */
public class MainApplication extends BaseApplication {
    @Override
    public void onCreate() {
        super.onCreate();

        // 初始化组件 Application
        initModuleApp(this);


        // 所有 Application 初始化后的操作
        initModuleData(this);

    }


    /**
     *  解决第三包 NoClassDefFoundError
     * @param base
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /**
     * 扫描BaseAppConfig里的子module通过反射初始化
     * @param application
     */
    @Override
    public void initModuleApp(Application application) {
        for (String moduleApp : BaseAppConfig.moduleApps) {
            try {
                Class clazz = Class.forName(moduleApp);
                BaseApplication baseApp = (BaseApplication) clazz.newInstance();
                baseApp.initModuleApp(application);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 扫描BaseAppConfig的包名列表通过反射初始化
     * @param application
     */
    @Override
    public void initModuleData(Application application) {
        for (String moduleApp : BaseAppConfig.moduleApps) {
            try {
                Class clazz = Class.forName(moduleApp);
                BaseApplication baseApp = (BaseApplication) clazz.newInstance();
                baseApp.initModuleData(application);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
    }


}
